import {computeNormals, f4} from "./primitiveUtils";
import {VertexData} from "../core/vertexData";

export function cube(width: number = 1, height: number = 1, depth: number = 1): VertexData {
    let width_half = width / 2;
    let height_half = height / 2;
    let depth_half = depth / 2;

    let data : {
        positions: number[],
        normals: number[],
        texCoords: number[],
        indices: number[]
    } = {
        positions: [],
        normals: [],
        texCoords: [],
        indices: []
    };

    //  顶点
    data.positions.push(width_half, height_half, -depth_half);
    data.positions.push(width_half, -height_half, -depth_half);
    data.positions.push(-width_half, -height_half, -depth_half);
    data.positions.push(-width_half, height_half, -depth_half);
    data.positions.push(width_half, height_half, depth_half);
    data.positions.push(width_half, -height_half, depth_half);
    data.positions.push(-width_half, -height_half, depth_half);
    data.positions.push(-width_half, height_half, depth_half);

    // 索引
    data.indices.push(...f4(0, 1, 2, 3));
    data.indices.push(...f4(4, 7, 6, 5));
    data.indices.push(...f4(0, 4, 5, 1));
    data.indices.push(...f4(1, 5, 6, 2));
    data.indices.push(...f4(2, 6, 7, 3));
    data.indices.push(...f4(4, 0, 3, 7));

    computeNormals(data);

    // fill uv with 0
    data.texCoords.length = data.positions.length * 2 / 3;
    data.texCoords.fill(0,0,data.texCoords.length);

    return new VertexData(data);
}
